﻿/*-------------------------------------
//
//Copyright (C) 2007 开发二组 版权所有。
//
//文件名：Product.ascx.cs
//
//
//文件功能描述：显示前台首页面的前8件商品.
//
//
//创建标识：2007.11.16 王勇
//创建描述：对商品展示的部分描述..
//
//------------------------------------*/

using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

public partial class Common_Controls_Product : System.Web.UI.UserControl
{

    //服务操作的类
    localhost.ProductService myOperate = new localhost.ProductService();


    private string typeProduct;  //绑定商品类型

    /// <summary>
    /// 商品类型的属性
    /// </summary>
    public string TypeProduct
    {
        set { typeProduct = value; }
        get
        {

            //临时变量
            string strTemp = string.Empty;

            switch (typeProduct)
            {
                case "热卖商品":
                    strTemp = "HotProduct";
                    break;
                case "推荐商品":
                    strTemp = "CommandProduct";
                    break;
                case "最新商品":
                    strTemp = "NewProduct";
                    break;
                default:
                    strTemp = typeProduct;
                    break;
            }
            return strTemp;
        }
    }
    
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
           //第一次加载进行数据绑定
            BindDataList();

            lblLatestProducts.Text = typeProduct;
        }
    }


   /// <summary>
   /// 数据绑定的方法
   /// </summary>
    protected void BindDataList()
    {
        //异步调用WebService来获取数据
        myOperate.GetProductListByProductTypeTop8Completed += new localhost.GetProductListByProductTypeTop8CompletedEventHandler(myOperate_GetProductListByProductTypeTop8Completed);

        myOperate.GetProductListByProductTypeTop8Async(TypeProduct);
        
    }

   
    /// <summary>
    /// 回调函数
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    void myOperate_GetProductListByProductTypeTop8Completed(object sender, localhost.GetProductListByProductTypeTop8CompletedEventArgs e)
    {
        //定义DataSet用来获取数据集
        DataSet dsProduct = new DataSet();

        //获取结果
        dsProduct = e.Result;

        //数据绑定
        dlstLatestProducts.DataSource = dsProduct;
        dlstLatestProducts.DataBind();
    }


    /// <summary>
    /// DataList数据项绑定事件
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void dlstLatestProducts_ItemDataBound(object sender, DataListItemEventArgs e)
    {
        //获取商品编号
        string strProductID = ((Label)e.Item.FindControl("lblProductID")).Text;

        //控制图片的大小
        ((ImageButton)e.Item.FindControl("imageBtnProducter")).Width = Unit.Parse("110px");
        ((ImageButton)e.Item.FindControl("imageBtnProducter")).Height = Unit.Parse("110px");

        //指定图片的ImageUrl
        ((ImageButton)e.Item.FindControl("imageBtnProducter")).ImageUrl = "../ImagInfo.aspx?productID=" + strProductID;

        //指定图片的超联结Url
        ((ImageButton)e.Item.FindControl("imageBtnProducter")).PostBackUrl = "../ProductInfo.aspx?productID=" + strProductID;
    }


    /// <summary>
    /// 查看更多商品的按钮事件
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void ibtnMoreProducts_Click(object sender, ImageClickEventArgs e)
    {
        //根据前台显示商品类型的值来确定，所以进行了硬编码
        switch (TypeProduct)
        {
            case "NewProduct":
                Response.Redirect("NewProduct.aspx");
                break;
            case "CommandProduct":
                Response.Redirect("CommendProduct.aspx");
                break;
            case "HotProduct":
                Response.Redirect("HotProduct.aspx");
                break;
        }
    }
}
